/**
 * jquery提示信息插件
 */
(function($) { 
    // #####################私有属性######################
    var private_property = {
        obj_bg: {}, //背景遮盖对象
        obj: {}, //内容对象
        btns: {}, //按钮对象
        default_set: {//默认设置
            message: {  title: "消息标题", content: "消息内容"},
            button: { /**sure: 'hide', cancel: 'hide'*/},
            show: {position: 'center', animate: 'default', show_speed: 200, hide_speed: 'normal'},
            auto_hide:{flag:false,time:3,btn_hide:false},
            callback:{start:function(){},end:function(){}}
        }
    };
    // #####################别名设置######################
    var MESSAGE = private_property.default_set.message;
    var BUTTON = private_property.default_set.button;
    var CALLBACK = private_property.default_set.callback;
    var SHOW = private_property.default_set.show;
    var OBJ = private_property.obj;
    var OBJ_BG = private_property.obj_bg;
    var BTNS = private_property.btns;
    var AUTO_HIDE = private_property.default_set.auto_hide;
    var PLAN;
    /**
     * ##################私有的方法##################
     * @type type
     */
    var private_method = {
        default_show:function(){
            OBJ.animate({top:OBJ.attr('top')},SHOW.show_speed,CALLBACK.start);
            if(AUTO_HIDE.flag){
                PLAN = setInterval(private_method.hide,AUTO_HIDE.time*1000);
            }
        },
        show: function() {
            if (SHOW.animate === 'default') {
                OBJ_BG.slideDown(SHOW.show_speed,private_method.default_show);
            }
        },
        hide:function(){
             if (SHOW.animate === 'default') {
                OBJ.animate({top:-OBJ.height()},SHOW.hide_speed,private_method.default_hide);
            }
        },
        default_hide: function() {
            OBJ_BG.fadeOut(SHOW.show_speed,CALLBACK.end);
            private_method.destroy();
        },
        destroy:function(){
            OBJ.remove();
            OBJ_BG.remove();
            clearInterval(PLAN);
        },
        draw: function() {
            var str = "<div  class='notice_box'><div class='notice_mid'><div class='notice_title'></div><div class='notice_con'></div><div class='notice_btn'></div></div></div>";
            var str_bg = "<div class='notice_bg'></div>";
            OBJ_BG = $(str_bg).appendTo('body');
            OBJ = $(str).appendTo('body');
        },
        fill: function() {
            OBJ.find('.notice_title').text(MESSAGE.title);
            OBJ.find('.notice_con').text(MESSAGE.content);
            if(AUTO_HIDE.btn_hide){
                return;
            }
            var num = 0;
            for (var i in BUTTON) {
                var btn_str = "<div class='btn'>" + i + "</div>";
                BTNS['btn' + num] = $(btn_str).appendTo(OBJ.find('.notice_btn'));

                //为按钮绑定事件
                if ($.isFunction(BUTTON[i])) {
                    BTNS['btn' + num].on('click', BUTTON[i]);
                } else if (BUTTON[i] in private_method) {
                    BTNS['btn' + num].on('click', private_method[BUTTON[i]]);
                } else {
                    $.error("按钮绑定函数错误！");
                }

                BTNS['btn' + num].on('mouseenter', function() {
                    $(this).addClass('btnover');
                });
                BTNS['btn' + num].on('mouseleave', function() {
                    $(this).removeClass('btnover');
                });

                num++;
            }
        },
        resize: function() {
            OBJ_BG.width($(document).width());
            OBJ_BG.height($(document).height());
            var top;
            if (SHOW.position === 'center') {
                top = ($(document).height() - OBJ.height()) / 2;
            } else if (0) {
                top = '';
            } else {
                top = '';
            }
            OBJ.attr('top', top);
        }
    };
    /**
     * ##################共有的方法##################
     * @type type
     */
    var public_method = {
        init: function(Notice, Args) {
            if ($.isPlainObject(Args[0]) || !Args.length) {
                private_property.default_set = $.extend(true, private_property.default_set, Args[0]);
            } else {
                $.error('消息提示插件参数不正确');
                return;
            }
            private_method.draw();
            private_method.fill();
            private_method.resize();
            private_method.show();
        }
    };
    
    $.Notice = function() {
        //共有属性在此设置

        public_method.init(this, arguments);

        };
})(jQuery);